package com.jz.pay.config;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.jz.pay.bo.CommonCacheBO;
import com.jz.pay.socket.WebSocketServer;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Order(1)
@Component
public class StartupRunner implements CommandLineRunner {

    @Resource
    CommonCacheBO commonCacheBO;

    @Resource
    public RedisProperties redisProperties;
    @Resource
    public WebSocketServer webSocketServer;

    @Override
    public void run(String... args) throws Exception {
        System.out.println("服务器启动成功");

        System.out.println("启动完成后将热点数据存放redis");
        System.out.println("=============================");
        commonCacheBO.cachePayment();
        commonCacheBO.getCachePayment();
//        commonCacheBO.delCachePayment();
        System.out.println("=============================");
        configSentinel();

//        webSocketServer.start();

    }

    private void configSentinel(){
            // 配置规则示例
            List<FlowRule> rules = new ArrayList<>();
            FlowRule rule = new FlowRule();
            rule.setResource("my_resource");
            rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
            rule.setCount(3);
            rules.add(rule);

            FlowRuleManager.loadRules(rules);
    }


}
